package is.glif.fenius.client.connection;

import static java.util.Arrays.asList;

import org.apache.log4j.Logger;

import joptsimple.OptionParser;
import joptsimple.OptionSet;

public class Invoker {
    private static Logger log = Logger.getLogger(Invoker.class);
    public static void main(String[] args) throws Exception {
        OptionParser parser = new OptionParser() {
            {
                acceptsAll( asList( "h", "?" ), "show help then exit" );
                accepts( "help", "show extended help then exit" );
                accepts( "r" , "/path/to/reservation spec file" ).withRequiredArg().ofType(String.class);
                accepts( "s" , "/path/to/server spec file" ).withRequiredArg().ofType(String.class);
                accepts( "l" , "/path/to/list spec file" ).withRequiredArg().ofType(String.class);
                accepts( "x" , "/path/to/releasespec file" ).withRequiredArg().ofType(String.class);
                accepts( "q" , "/path/to/queryspec file" ).withRequiredArg().ofType(String.class);
            }
        };
    
        OptionSet options = parser.parse( args );
    
        // check for help
        if ( options.has( "?" ) || options.has("h") || options.has("help")) {
            parser.printHelpOn( System.out );
            System.exit(0);
        }
        
        ConfigHolder h = ConfigHolder.getInstance();
        
        if (options.has("r")) {
            h.setResvsConfigFile((String) options.valueOf("r"));
            log.debug("-r "+(String) options.valueOf("r"));
        }
        if (options.has("x")) {
            h.setReleasesConfigFile((String) options.valueOf("x"));
            log.debug("-x "+(String) options.valueOf("x"));
        }
        if (options.has("l")) {
            h.setListsConfigFile((String) options.valueOf("l"));
            log.debug("-l "+(String) options.valueOf("l"));
        }
        if (options.has("s")) {
            h.setServersConfigFile((String) options.valueOf("s"));
            log.debug("-s "+(String) options.valueOf("s"));
        }
        if (options.has("q")) {
            h.setQueriesConfigFile((String) options.valueOf("q"));
            log.debug("-q "+(String) options.valueOf("q"));
        }
        h.loadConfig();
        
        Requester req = Requester.getInstance();
        if (options.has("x")) {
            req.release();
        } else if (options.has("q")) {
            req.query();
        } else if (options.has("l")) {
            req.list();
        } else if (options.has("r")) {
            req.reserve();
        }
        
    }

}
